package com.fowo.api.ptt.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.fowo.api.common.model.RException;
import com.fowo.api.ptt.enitty.PrintTemplateGroup;
import com.fowo.api.ptt.enitty.PrintTemplateType;
import com.fowo.api.ptt.mapper.PrintTemplateGroupMapper;
import com.fowo.api.ptt.mapper.PrintTemplateTypeMapper;
import com.fowo.api.ptt.service.PrintTemplateGroupService;
import com.fowo.api.user.model.JwtUserInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 打印组服务实现
 *
 * @author yl_ls
 */
@Service
public class PrintTemplateGroupServiceImpl implements PrintTemplateGroupService {
    @Resource
    private PrintTemplateGroupMapper groupMapper;
    @Resource
    private PrintTemplateTypeMapper typeMapper;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long create(PrintTemplateGroup printTemplateGroup) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo == null) {
            throw new RException("操作者身份必须提供");
        }
        if (printTemplateGroup.getGroupName() == null) {
            throw new RException("必须设置组名称");
        }
        Date now = new Date();
        printTemplateGroup.setCreateUser(jwtUserInfo.getUserId());
        printTemplateGroup.setCreateTime(now);
        printTemplateGroup.setLastUpdateUser(jwtUserInfo.getUserId());
        printTemplateGroup.setLastUpdateTime(now);
        printTemplateGroup.setDelFlag(false);

        if (groupMapper.insert(printTemplateGroup) != 1) {
            throw new RException("插入数据失败");
        }
        return printTemplateGroup.getId();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(PrintTemplateGroup printTemplateGroup) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo == null) {
            throw new RException("操作者身份必须提供");
        }
        PrintTemplateGroup rawTemplate = groupMapper.selectById(printTemplateGroup.getId());
        if (rawTemplate == null) {
            throw new RException("未找到要更新的打印组");
        }
        Date now = new Date();
        printTemplateGroup.setLastUpdateUser(jwtUserInfo.getUserId());
        printTemplateGroup.setLastUpdateTime(now);

        if (groupMapper.updateById(printTemplateGroup) != 1) {
            throw new RException("更新数据失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Long id) {
        final PrintTemplateGroup rawTemplate = groupMapper.selectById(id);
        if (rawTemplate == null) {
            throw new RException("未找到要更新的打印组");
        }
        final List<PrintTemplateType> typeList = typeMapper.selectByGroupId(id);
        if (CollUtil.isEmpty(typeList)) {
            throw new RException("分组下包含打印类型,请先删除!");
        }
        if (groupMapper.deleteById(id) != 1) {
            throw new RException("删除数据失败");
        }
    }

}
